package com.gdth.bank.activity;

import java.util.ArrayList;
import java.util.List;

import com.gdth.bank.BuildConfig;
import com.gdth.bank.R;
import com.gdth.bank.adapter.GncdListAdapter;
import com.gdth.bank.presenter.SynchronizePresenter;
import com.gdth.bank.utils.MenuUtil;
import com.gdth.bank.view.HuoDongDjView;
import com.gdth.bank.widget.DialogList;
import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseActivity;
import com.gdth.model.ImageItem;
import com.gdth.resource.Constant;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 功能菜单页面
 * @author mac1
 *
 */
public class MenuListActivity extends BaseActivity implements HuoDongDjView{

	@ViewInject(id=R.id.header_label_title) TextView txt_header_title;
	@ViewInject(id=R.id.header_btn_left, click="finishClick") TextView txt_header_left;
    @ViewInject(id=R.id.header_btn_right) TextView txt_header_right;
    
    @ViewInject(id=R.id.menu_listview, itemClick="onItemClick") ListView listview_menu;
    
    List<ImageItem> items,hddj;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu_list);
		SynchronizePresenter presenter = new SynchronizePresenter(this);
		String content="";
		if(Constant.banktype == 1)// 只有攸县有活动登记
			presenter.gethddj(content);
		initView();
		
	}

	private void initView() {
		txt_header_right.setVisibility(View.GONE);
		String key = this.getIntent().getStringExtra("key");
		String name = this.getIntent().getStringExtra("name");
		txt_header_title.setText(name);
		
		items = MenuUtil.getMenus(key);
		
		GncdListAdapter adapter = new GncdListAdapter(this, items);
		
		listview_menu.setAdapter(adapter);
	}
	
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
    	ImageItem mImageItem = items.get(arg2);
    	joinToActivity(mImageItem);
	}
	
	private void joinToActivity(ImageItem item){
		if(item.getGnlj().toString().equals("dzcpdj")){
			DialogList dialog=new DialogList(this, "请选择电子产品登记类型", false);
			List<ImageItem> items = new ArrayList<ImageItem>();
			ImageItem item1=new ImageItem();
			item1.setValue("ETC");
			item1.setKey("com.gdth.bank.activity.DzCpKhXxEtcActivity");
			item1.setImg("dzcp/dzcp_etc.png");
			items.add(item1);
			ImageItem item2=new ImageItem();
			item2.setValue("POS");
			item2.setKey("com.gdth.bank.activity.DzCpKhXxPosActivity");
			item2.setImg("dzcp/dzcp_pos.png");
			items.add(item2);
			dialog.setListItem(items);
			dialog.show();
		}else if(item.getGnlj().toString().equals("hddj")){
			DialogList dialog=new DialogList(this, "请选择活动类型", false);
			List<ImageItem> items = hddj;
			for(int i=0;i<items.size();i++){
				ImageItem mImageItem=new ImageItem();
				mImageItem.setGnlj("hddjxx");
				mImageItem.setKey(items.get(i).getKey());
				mImageItem.setValue(items.get(i).getValue());
			}
			dialog.setListItem(items);
			dialog.show();
		}else if(item.getGnlj().toString().equals("zmlcd")){
			//子目录菜单
	        if (item.getType() == 2) {
	            Intent intent = new Intent();
	            intent.setClassName(this, item.getGnlj());
	            this.startActivity(intent);
	        } else if (item.getType() == 1) {
	            String gnlj = item.getGnlj();
	            String[] strs = gnlj.split("\\?");
	            if (gnlj.startsWith("com.gdth.bank.activity.WebViewActivity")) {
	                Intent intent = new Intent(this, WebViewActivity.class);
	                intent.putExtra("path", strs[1].toString());
	                intent.putExtra("name", item.getValue());
	                this.startActivity(intent);
	            } else {
	                Intent intent = new Intent(this, MenuListActivity.class);
	                intent.putExtra("key", item.getKey());
	                intent.putExtra("name", item.getValue());

	                this.startActivity(intent);
	            }

	        }
		}
		else{
	    	Intent intent = new Intent();
	    	String gnlj = item.getGnlj();
	    	String[] strs = gnlj.split("\\?");
	    	
	    	if (strs.length > 1) {
	    		if(strs.length > 2){
	    			String url = "";
//	    			for (int i = 1;i<=strs.length;i++) {
//						url += strs[i] ;
//						if(i!=strs.length){
//							url += "?";
//						}
//					}
	    			url = gnlj.replace(strs[0]+"?", "");
	    			intent.putExtra("path", url);
	    		}else{
	    			intent.putExtra("path", strs[1].toString());
	    		}
	    		intent.putExtra("name", item.getValue());
	    	}
	    	intent.setClassName(this, strs[0].toString());
			startActivity(intent);
		}
    }

	@Override
	public void updateHddj(List<ImageItem> arg0) {
		// TODO Auto-generated method stub
		
		hddj = arg0;
//		for(int i=0;i<arg0.size();i++){
//			ImageItem mImageItem=new ImageItem();
//			mImageItem.setKey(items.get(i).getKey());
//			mImageItem.setValue(items.get(i).getValue());
//			dialog.setListItem(items);
//		}
//		dialog.show();
	}

	@Override
	public void showProgressDialog(String msg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hideProgressDialog() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void showMessage(String msg) {
		// TODO Auto-generated method stub
		
	}

	

	
}
